/*
    This file is part of Titan.

    Titan is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as 
    published by the Free Software Foundation, either version 3 of 
    the License, or (at your option) any later version.

    Titan is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Titan. If not, see <http://www.gnu.org/licenses/>.
*/

package titancommon.applications;


import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;

/*
 * ShowGraphics.java
 *
 * Created on 5. August 2008, 13:59
 */
/**
 *
 * @author  Mirco Rossi <rossi@ife.ee.ethz.ch>
 */
public class DiceFrame extends java.awt.Frame {

    /** Display states */
    public static final int DICE_NEUTRAL  = 0;
    public static final int DICE_PICKEDUP = 1;
    public static final int DICE_SHAKE  = 2;
    public static final int DICE_THROW  = 3;
    public static final int DICE_SCORE  = 4;
    
    Image m_img[];
    int m_scores[];
    int m_states[];

    String m_PicFolder;
    Dice m_dice;
    
    public class NextPlayerMsg {
      
    }

    
    
    /** Creates new form ShowGraphics */
    public DiceFrame(Dice d) {
        
        initComponents();
        m_img=new Image[6];
        m_scores=new int[6];
        m_states=new int[6];
             
        m_PicFolder="data/pics/";  
        for(int i=0;i<6;i++){
            setDice(i+1,0,0);
            m_scores[i]=0;
            m_states[i]=0;
        }        
        
        m_dice=d;
        
    }

    /**
     * Sets the images on the frame to the current dice state
     * @param dice Dice ID to be set values (0-5)
     * @param eyes Number of eyes to show (1-6)
     * @param state Dice state. Can be one of DICE_NEUTRAL, DICE_PICKEDUP, DICE_BLOCKED
     * @return Whether the dice could be set to the given state
     */
    public boolean setDice(int dice, int eyes, int state) {
        
        if (dice >= m_img.length) return false;
        
        String stateName="_white_";
        String sFile;
        switch(state){
                case 0:
                    sFile= m_PicFolder +"dice" + "_white_"+ Integer.toString(eyes)+".gif";  break;
                case 1:
                     sFile= m_PicFolder + "dice_pickup.gif"; break;
                case 2:
                     sFile= m_PicFolder + "dice_shake.gif"; break;
                case 3:
                     sFile= m_PicFolder + "dice_throw.gif"; break;
                case 4:
                    sFile= m_PicFolder +"dice" + "_red_"+ Integer.toString(eyes)+".gif";  break;
            default:
                return false;
        }
                         
        m_img[dice] = getToolkit().getImage(sFile);
        updateCanvas(dice);
        
        m_scores[dice]=eyes;
        m_states[dice]=state;
        
        return true;
    }
    
    public void setMessage(String strMessage) {
        playerMessage.setText(strMessage);
    }
    
    public boolean setScore(int iPlayer, int iTotalScore) {
       boolean bResult = true;
       switch(iPlayer) {
          case 0: 
                  totalScore1.setText(""+iTotalScore);
                  break;
          case 1: 
                  totalScore2.setText(""+iTotalScore);
          default:
             bResult = false;
       }
       return bResult;
    }
    

    private void updateCanvas(int dice){
        switch(dice){
            case 0: canvas1.repaint();break;
            case 1: canvas2.repaint();break;
            case 2: canvas3.repaint();break;
            case 3: canvas4.repaint();break;
        }
    }
    
    public void setTextBold(int p){
        if(p==1){
            Fix2Label.setFont(new Font("Dialog",Font.PLAIN,12));
            Fix2Label1.setFont(new Font("Dialog",Font.BOLD,12));
        }else{
            Fix2Label.setFont(new Font("Dialog",Font.BOLD,12));
            Fix2Label1.setFont(new Font("Dialog",Font.PLAIN,12));            
            
            
        }
        
    }
            
        
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        canvas1 = new java.awt.Canvas() {
            public void paint( Graphics g )
            {
                g.drawImage( m_img[0], 0, 0, this );
            }

        };
        Fix1Label = new java.awt.Label();
        Fix2Label = new java.awt.Label();
        canvas2 = new java.awt.Canvas() {
            public void paint( Graphics g )
            {
                g.drawImage( m_img[1], 0, 0, this );
            }

        };
        canvas3 = new java.awt.Canvas() {
            public void paint( Graphics g )
            {
                g.drawImage( m_img[2], 0, 0, this );
            }

        };
        canvas4 = new java.awt.Canvas() {
            public void paint( Graphics g )
            {
                g.drawImage( m_img[3], 0, 0, this );
            }

        };
        Fix2Label1 = new java.awt.Label();
        playerMessage = new java.awt.Label();
        totalScore1 = new java.awt.Label();
        totalScore2 = new java.awt.Label();
        button1 = new java.awt.Button();
        button2 = new java.awt.Button();
        checkbox1 = new java.awt.Checkbox();

        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        Fix1Label.setAlignment(java.awt.Label.CENTER);
        Fix1Label.setBackground(new java.awt.Color(51, 102, 255));
        Fix1Label.setFont(new java.awt.Font("Arial", 1, 20));
        Fix1Label.setForeground(new java.awt.Color(255, 255, 255));
        Fix1Label.setText("Würfelspiel");

        Fix2Label.setForeground(new java.awt.Color(0, 0, 0));
        Fix2Label.setText("Spieler 1:");

        Fix2Label1.setText("Spieler 2:");

        playerMessage.setAlignment(java.awt.Label.CENTER);
        playerMessage.setName("Message"); // NOI18N
        playerMessage.setText("Initialisiere Würfelspiel");

        totalScore1.setAlignment(java.awt.Label.RIGHT);
        totalScore1.setName("totalScore1"); // NOI18N
        totalScore1.setText("0");

        totalScore2.setAlignment(java.awt.Label.RIGHT);
        totalScore2.setName("totalScore2"); // NOI18N
        totalScore2.setText("0");

        button1.setLabel("schliessen");
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButonActionPerformed(evt);
            }
        });

        button2.setLabel("reset");
        button2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button2ActionPerformed(evt);
            }
        });

        checkbox1.setLabel("nur Augenzahl");
        checkbox1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                checkbox1MouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, Fix1Label, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .add(33, 33, 33)
                .add(Fix2Label, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(156, 156, 156))
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(playerMessage, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)
                .addContainerGap())
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(button1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(button2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(checkbox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .add(layout.createSequentialGroup()
                .add(33, 33, 33)
                .add(Fix2Label1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(70, 70, 70)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                    .add(totalScore1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 62, Short.MAX_VALUE)
                    .add(totalScore2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 62, Short.MAX_VALUE))
                .addContainerGap(44, Short.MAX_VALUE))
            .add(layout.createSequentialGroup()
                .addContainerGap(81, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(canvas3, 0, 0, Short.MAX_VALUE)
                    .add(canvas1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(canvas4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(canvas2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(74, 74, 74))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(Fix1Label, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(Fix2Label, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(totalScore1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(Fix2Label1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(totalScore2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(canvas1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(canvas2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(canvas4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(canvas3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(playerMessage, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(button1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(button2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(checkbox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        System.exit(0);
    }//GEN-LAST:event_exitForm

private void closeButonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButonActionPerformed
   this.setVisible(false);
}//GEN-LAST:event_closeButonActionPerformed

private void buttonNextPlayercloseButonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonNextPlayercloseButonActionPerformed
  titancommon.TitanCommand.singleton.m_msgDispatcher.sendMessage(new NextPlayerMsg());
}//GEN-LAST:event_buttonNextPlayercloseButonActionPerformed

private void button2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button2ActionPerformed
 m_dice.restart();
}//GEN-LAST:event_button2ActionPerformed

private void checkbox1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_checkbox1MouseClicked
 m_dice.scoreOnly(checkbox1.getState());


}//GEN-LAST:event_checkbox1MouseClicked
  


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FarkleFrame().setVisible(true);
            }
        });
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Label Fix1Label;
    private java.awt.Label Fix2Label;
    private java.awt.Label Fix2Label1;
    private java.awt.Button button1;
    private java.awt.Button button2;
    private java.awt.Canvas canvas1;
    private java.awt.Canvas canvas2;
    private java.awt.Canvas canvas3;
    private java.awt.Canvas canvas4;
    private java.awt.Checkbox checkbox1;
    private java.awt.Label playerMessage;
    private java.awt.Label totalScore1;
    private java.awt.Label totalScore2;
    // End of variables declaration//GEN-END:variables

}
